package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.DBUtil;
import com.hsbc.dev.teamo4.sms.sm.dao.IUserDao;
import com.hsbc.dev.teamo4.sms.sm.po.User;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;

/**
 * @author:Nina
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Description:
 * @Date date: 2018/7/25
 */
public class UserDaoImpl implements IUserDao {
    public UserDaoImpl(){};
    /**
     * save User
     * @param user
     * @return User
     */
    @Override
    public User saveEntity(User user){
        String sql="insert into sm_user values(?,?,?,?,?,?,?,?,?,?,?)";
        return (User) DBUtil.saveEntity(sql,user,null);
    }
    /**
     * updateEntity User
     * @param user
     * @return User
     */
    @Override
    public User updateEntity(User user) {
        String sql="update sm_user set name=?,userSex=?,userPhone=?,userEmail=?,accountId=?," +
                "updateId=?,updateTime=?,isEnable=? where id=?";
        return (User) DBUtil.updateEntity(sql,user,Lists.newArrayList("id","createId","createTime"),user.getId());
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        String sql = "update sm_user set isEnable=? where id=?";
        return DBUtil.updateByIsEnable(sql,id,bool);
    }

    /**
     * query User By Id
     * @param id
     * @return User
     */
    @Override
    public User queryById(String id) {
        String sql="select * from sm_user where id='"+id+"'";
        BeanHandler beanHandler=new BeanHandler<>(User.class);
        return (User) DBUtil.queryById(sql,beanHandler);
    }

    /**
     * query User by All User
     * @return
     */
    @Override
    public List<User> queryAllEntity() {
        String sql="select * from sm_user where isEnable='"+true+"'";
        BeanListHandler beanListHandler=new BeanListHandler<>(User.class);
        return (List<User>) DBUtil.queryAllEntity(sql,beanListHandler);

    }

    /**
     * @Description 通过账户id获取用户信息
     * @Date: 21:00 2018-07-27
     * @Param accountId
     * @return com.hsbc.dev.teamo4.sms.sm.po.User
     */
    @Override
    public User queryByAccountId(String accountId){
        String sql="select * from sm_user where accountId='"+accountId+"'";
        BeanHandler beanHandler=new BeanHandler<>(User.class);
        return (User) DBUtil.queryById(sql,beanHandler);
    }

    /**
     * @param name
     * @return
     * @Description 根据用户名进行模糊查询
     * @Date 10:00 2018-07-29
     */
    @Override
    public List<User> querylikeName(String name) {
        String sql="select * from sm_user where name like '%"+name+"%'";
        BeanListHandler beanListHandler=new BeanListHandler<>(User.class);
        return (List<User>) DBUtil.queryAllEntity(sql,beanListHandler);
    }


}
